package com.nifostasky.comic;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.nifostasky.entity.ComicInfo;
import com.nifostasky.utils.JsoupContent;

import java.io.Serializable;

public class ChapterActivity extends AppCompatActivity {
    //声明控件
    private ImageView miv_comicPicture = null;
    private TextView mtv_comicName = null;
    private ListView mlv_chapter = null;
    //声明爬取数据的对象
    private JsoupContent jsoupContent = null ;
    //漫画信息
    private ComicInfo comicInfo= null;
    //漫画网址
    private String url = null;
    //判断漫画是否获取成功
    boolean is = false;
    boolean istwo = false;
    //缓冲提示条
//    private ProgressDialog progreesDiaLog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter);
        /*setProgressDialog();*/
        //获取控件
        miv_comicPicture = findViewById(R.id.im_comicPicture);
        mtv_comicName = findViewById(R.id.tv_comicName);
        mlv_chapter = findViewById(R.id.lv_chapter);

        //获取url
        Intent intent  = getIntent();
        url = intent.getStringExtra("url");
        System.out.println(url);
        System.out.println("执行完毕0");

        //获取章节
        getChapterAll();
        showComicPictureAndName();
        /*//关闭缓冲条
        progreesDiaLog.dismiss();*/
    }


    //获取所有的章节
    private void getChapterAll(){
        Runnable runnable = new Runnable(){

            @Override
            public void run() {
                ChapterActivity.this.jsoupContent = new JsoupContent(url);
                //获取章节信息
                ChapterActivity.this.jsoupContent.setChapterName();
                if(jsoupContent.isFlag()) {
                    ChapterActivity.this.comicInfo = jsoupContent.getComicinfo();

                    System.out.println("执行完毕1");
                    ChapterActivity.this.istwo = true;
                    ChapterActivity.this.is = true;//ChapterActivity.this.jsoupContent.isFlag();
                }else{
                    System.out.println("执行完毕9");
                    ChapterActivity.this.istwo = false;
                    ChapterActivity.this.is = true;
                }
            }
        };
        new Thread(runnable).start();
//        mlv_chapter.setAdapter(new MyAdapter());
        while(true) {
            if(ChapterActivity.this.is){
                if(ChapterActivity.this.istwo) {
                    System.out.println("执行完毕");
                    mlv_chapter.setAdapter(new MyAdapter());
                }else{
                    Toast.makeText(this, "你输入的网址不能解析,请换一个试试" , Toast.LENGTH_SHORT).show();
                }
                ChapterActivity.this.is = false;
                ChapterActivity.this.istwo = false;
                break;
            }
        }
        System.out.println("执行完毕2");

    }





    //适配器
    private class MyAdapter extends BaseAdapter{
        //总共有多少条数据
        @Override
        public int getCount() {
            System.out.println("总共的数目:"+ChapterActivity.this.comicInfo.getChapterNumber());
            return ChapterActivity.this.comicInfo.getChapterNumber();
        }


        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = null;
            if(convertView == null) {
                view = View.inflate(ChapterActivity.this,R.layout.layout,null);
                //view = new TextView(ChapterActivity.this);
            }else{
                view = convertView;
            }
            //获取控件
            TextView tv = view.findViewById(R.id.tv_Chaptername);
            TextView tv2 = view.findViewById(R.id.tv_number);
            //设置文本
            tv.setText(comicInfo.getChapterName().get(position)[0]);
            tv2.setText((position+1)+"");
//            System.out.println("章节 : "+comicInfo.getChapterName().get(position)[0]);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.out.println("按键被点击");
                    //定义意图并,确定转发位置
                    Intent intent = new Intent();
                    intent.setClass(ChapterActivity.this, ComicPicture.class);
                    //设置需要传递的参数
                    intent.putExtra("comicInfo",comicInfo);
                    intent.putExtra("position", position);
                    //执行意图进行跳转
                    startActivity(intent);
                }
            });
            return view;
        }


        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }


    }

    //将漫画名和图片显示到组件中
    private void showComicPictureAndName(){
        if(comicInfo != null && comicInfo.getComicName() != null) {
            mtv_comicName.setText(comicInfo.getComicName());
        }else{
            mtv_comicName.setText("未识别到漫画名");
        }
        if(comicInfo != null && comicInfo.getSurfacePlotUrl() != null){
            System.out.println(comicInfo.getSurfacePlotUrl());
            Glide.with(this).load(comicInfo.getSurfacePlotUrl()).into(miv_comicPicture);
        }
    }


/*


    //设置消息缓冲提示框
    private void setProgressDialog(){
        //设置缓冲提示框
        progreesDiaLog = new ProgressDialog(ChapterActivity.this);
        progreesDiaLog.setTitle("提示");
        progreesDiaLog.setMessage("正在加载");
        progreesDiaLog.setCancelable(false);
        progreesDiaLog.show();
    }
*/


    public ComicInfo getComicInfo() {
        return comicInfo;
    }

    public void setComicInfo(ComicInfo comicInfo) {
        this.comicInfo = comicInfo;
    }
}



